<?php

error_reporting(0);

// http://dev.xianglephp/_test/memcached

class Controller_Test_Memcached extends Controller_Test_Memcache
{
    public function init()
    {
        $this->_cache = new Com_Cache_Memcache('test_pecl_memcached');
    }

    public function getAction()
    {
        echo 12;
        exit;
        var_dump($this->_cache->get('testcb12', array($this, 'user_info_cb')));
    }

    // callback 注意 expire 有效期为永久0
    public function user_info_cb($memc, $key, &$value)
    {
        $user_id = substr($key, 5);
        /* 从数据库读取个人信息 */
        /* ... */
        $value = time();
        return true;
    }

    public function casAction()
    {
        $this->_cache = new Memcached();
        $this->_cache->addServers(array('10.103.252.42', '11211'));
        var_dump($this->_cache->add('test_cas', time()));

        $v = $this->_cache->get('test_cas', null, $cas);
        var_dump($v);
        var_dump($cas);
        exit;
        var_dump($this->_cache->cas($cas, 'test_cas', uniqid()));
        var_dump($this->_cache->getResultCode());
    }

    public function serverKeyAction()
    {
        var_dump($this->_cache->getServerByKey('user'));
        var_dump($this->_cache->getServerByKey('log'));
        var_dump($this->_cache->getServerByKey('ip'));
    }

    public function failoverAction()
    {
        if ($this->get('set')) {
            $start = microtime(true);
            $s = 0;
            for ($i = 1; $i <= 10000; $i++) {
                $this->_cache->set('test_fail_over_' . $i, $i) && $s++;
            }
            echo 'set success: ' . $s . '<br />';
            echo number_format(microtime(true) - $start, 6, '.', '');
        }

        $start = microtime(true);
        $s = 0;
        for ($i = 1; $i <= 10000; $i++) {
            $this->_cache->get('test_fail_over_' . $i) && $s++;
        }
        echo 'get success: ' . $s . '<br />';
        echo number_format(microtime(true) - $start, 6, '.', '');
    }

    public function multiAction()
    {
        echo '<h1>-------mget/mset ---- ' . '</h1>';
        $items = array(
            'key1' => 'value1',
            'key2' => 'value2',
            'key3' => 'value3'
        );
        var_dump($this->_cache->setMulti($items));
        var_dump($this->_cache->getMulti(array_keys($items)));
        var_dump($this->_cache->getMulti(array_keys($items), $casTokens, 0));

        echo 'castokens:<br />';
        var_dump($casTokens);
        var_dump($this->_cache->getResultCode() == Memcached::RES_SUCCESS);

        $items[] = 'non1';
        $items[] = 'non2';
        $items[] = 'non3';
        var_dump($this->_cache->getMulti(array_keys($items)));
        var_dump($this->_cache->getResultCode());
    }

    public function orgAction()
    {
        $org = new Memcached();
        var_dump($org);
        $org->addServers(array(
            array('127.0.0.1', '11211'),
            array('192.168.2.155', '11211')
        ));
        var_dump($org);
        var_dump($org->getResultCode());
        var_dump($org->set('kkk', time(), 10));
        var_dump($org->get('kkk'));
    }
}